package com.atguigu.serviceedu.controller.frontController;

import com.atguigu.commonutils.JwtUtils;
import com.atguigu.commonutils.R;
import com.atguigu.serviceedu.client.OrderClient;
import com.atguigu.serviceedu.entity.EduCourse;
import com.atguigu.serviceedu.entity.chaptervo.ChapterInfoVo;
import com.atguigu.serviceedu.entity.coursevo.CourseQueryVo;
import com.atguigu.serviceedu.entity.coursevo.CourseWebVo;
import com.atguigu.serviceedu.entity.subjectvo.OneSubjectVo;
import com.atguigu.serviceedu.service.EduChapterService;
import com.atguigu.serviceedu.service.EduCourseService;
import com.atguigu.serviceedu.service.EduSubjectService;
import com.atguigu.serviceedu.service.EduTeacherService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

//@CrossOrigin
@RestController
@Api(description = "显示课程内容")
@RequestMapping("edu/front/course")
public class FrontCourseController {

    @Autowired
    EduCourseService courseService;
    @Autowired
    EduSubjectService subjectService;
    @Autowired
    EduChapterService chapterService;
    @Autowired
    OrderClient orderClient;


    @ApiOperation(value = "分页课程列表")
    @PostMapping("{page}/{limit}")
    public R pageList(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,

            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit,

            @ApiParam(name = "courseQuery", value = "查询对象", required = false)
            @RequestBody(required = false) CourseQueryVo courseQuery){
        Page<EduCourse> pageParam = new Page<>(page, limit);
        Map<String, Object> map = courseService.pageListWeb(pageParam, courseQuery);
        return  R.ok().data(map);
    }

    @GetMapping("getNestedTreeList")
    public R getAllSubject(){
        List<OneSubjectVo> oneSubjectVos = subjectService.findSubjectVos();
        return R.ok().data("items",oneSubjectVos);
    }

    @ApiOperation(value = "根据ID查询课程")
    @GetMapping(value = "getCourseWebInfo/{courseId}")
    public R getCourseWebInfoById(
            @ApiParam(name = "courseId", value = "课程ID", required = true)
            @PathVariable String courseId,
            HttpServletRequest request){

        //查询课程信息和讲师信息
        CourseWebVo courseWebVo = courseService.selectInfoWebById(courseId);

        //查询当前课程的章节信息
        List<ChapterInfoVo> chapterVoList = chapterService.getChapterList(courseId);

        //远程调用，判断课程是否被购买
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        boolean isBuy = false;
        if(!StringUtils.isEmpty(memberId)){
            isBuy = orderClient.isBuyCourse(memberId, courseId);
        }

        return R.ok().data("course", courseWebVo)
                .data("chapterVoList", chapterVoList)
                .data("isBuy",isBuy);
    }





}